<?php
DEFINE('CATEGORIES_TYPE'	, 9);
class Category extends Vfriend_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function add($intAdminId, $strName, $intParentId, $intDisOrder, $intType=CATEGORIES_TYPE) {
		try {	
			$this->prepare('call categories_add_admin(?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strName, PDO::PARAM_STR, 255);
			$this->bindParam(3, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intParentId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $intDisOrder, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intCategoryId = 0;
				$this->clear->deleteCategoryByType($intParentId, $intType);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Category::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function update($intAdminId, $intCategoryId, $strName, $intParentId, $intDisOrder, $intType=CATEGORIES_TYPE) {
		try {
			$this->prepare('call categories_update(?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName, 		PDO::PARAM_STR, 255);			
			$this->bindParam(4, $intParentId, 	PDO::PARAM_INT, 11);
			$this->bindParam(5, $intDisOrder, 	PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteCategoryById($intCategoryId);
				$this->clear->deleteCategoryByType($intParentId, $intType);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Category::update - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateStatus($intAdminId, $intCategoryId, $intStatus, $intPerm, $intType=CATEGORIES_TYPE) {
		try {
			$this->prepare('call categories_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $intStatus, 	PDO::PARAM_INT, 11);
			$this->bindParam(4, $intPerm, 		PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteCategoryById($intCategoryId);
				$this->clear->deleteCategoryByType($intCategoryId, $intType);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Category::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}
	public function delete($intAdminId, $intCategoryId, $intType=CATEGORIES_TYPE) {
		try {
			$this->prepare('call categories_delete(?,?,?)');
			$this->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intType, 		PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteCategoryById($intCategoryId);
				$this->clear->deleteCategoryByType($intCategoryId, $intType);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Category::delete - ' .$e->getMessage());			
			return null;
		}
	}
	public function get($intCategoryId) {
		try {	
			$strKey 	= BACKEND_CATEGORY_GET . $intCategoryId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call categories_get(?)');
				$this->bindParam(1, $intCategoryId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, BACKEND_CATEGORY_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Category::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intAdType, $intParentId, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call categories_getall_admin(?,?,?,?)');
			$this->bindParam(1, $intAdType, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intParentId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Category::getAll - ' .$e->getMessage());			
			return null;
		}
	}	
}
?>